/*
 * 컴파일을 위해 올려 놓은 소스 입니다.
 * 서버에 반영하지 마시고 다른국가에 적용할 경우 서버에 있는 class 를 카피해서 사용하십시오.
 * 
 */
/*
 * Created on 2004. 9. 24.
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package com.pushmsg.OmaOtaConf.OmaOtaObject;

import java.util.*;
/**
 * @author dgchun
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class NAPDEF 
{
	private Hashtable profile = null;
	private String country = null;
	private String operator = null;
	private String appType = null;
	private String bearer = null;
	private String nettype = null;	
	private String napid = null;
	private String name = null;
	private String nap_address = null;
	private String nap_addresstype = null;
	private String calltype = null;
	private String linkspeed = null;
	private String napauthinfo = null;	
	private String authname = null;
	private String authsecret = null;
	private String dns1 = null;
	private String dns2 = null;
	
	public NAPDEF(String country, String op, String appType, String bearer, String nettype)
	{
		this.country = country;
		this.operator = op;
		this.appType = appType;
		this.bearer = bearer;
		this.nettype = nettype;
		napid = operator.toUpperCase() + "-" + bearer.toUpperCase() + "-" + nettype.toUpperCase();
	}
	
	private boolean getSetting()
	{
		String file = null;
		System.out.println("NETTYPE=" + nettype);
		if(appType.equals("email")||nettype.equals("wap"))
			file = Constants.FILEPATH + country.toLowerCase() + "/" + operator + "_" + bearer + "_" + nettype + ".xml";
		else
			file = Constants.FILEPATH + country.toLowerCase() + "/" + operator + "_" + appType + "_" + bearer + "_" + nettype + ".xml";
		ProfileReader pr = new ProfileReader(file);
		profile = pr.getProfile();
		if(profile==null)
			return false;		
		
		//System.out.println("napid=" + napid);
		name = napid;
		if(bearer.equals("gprs"))
		{
			bearer = "GSM-GPRS";
			nap_address = (String)profile.get("GPRS_ACCESSPOINTNAME");
			nap_addresstype = "APN";			
		}
		else
		{
			bearer = "GSM-CSD";
			nap_address = (String)profile.get("CSD_DIALSTRING");
			nap_addresstype = "E164";
			calltype = "V.110";
			linkspeed = "9600";
		}
		
		napauthinfo = (String)profile.get("PPP_AUTHTYPE");
		authname = (String)profile.get("PPP_AUTHNAME");
		authsecret = (String)profile.get("PPP_AUTHSECRET");
		dns1 = (String)profile.get("DNS1");
		dns2 = (String)profile.get("DNS2");
		
		return true;		
	}
	
	public String getNapId()
	{
		return napid;
	}
	
	//Param 1 : the buffer for the data to be stored
	//Param 2 : the start position 
	//Return  : the number of data added to the buffer
	public int getData(byte[] buffer, int index)
	{
		getSetting();
		int startPos=index;
		//<characteristic type="NAPDEF">
		buffer[index++] = (byte)0xC6; //characteristic
		buffer[index++] = (byte)0x55; //type=NAPDEF
		buffer[index++] = (byte)0x01; //start of characteristic with type=NAPDEF
		//<param name="NAPID" value="XXXX"/>
		buffer[index++] = (byte)0x87; //start of param
		buffer[index++] = (byte)0x11; //name=NAPID
		buffer[index++] = (byte)0x06; //value=
		buffer[index++] = (byte)0x03; //start of inline string
		System.arraycopy(napid.getBytes(), 0, buffer, index, napid.length());
		index += napid.length();
		buffer[index++] = (byte)0x00; //end of inline string
		buffer[index++] = (byte)0x01; //end of param element
		//<param name="BEARER" value=""/>
		buffer[index++] = (byte)0x87; //start of param
		buffer[index++] = (byte)0x10; //name=BEARER
		buffer[index++] = (byte)0x06; //value=
		if(bearer.equals("GSM-GPRS"))
			buffer[index++] = (byte)0xAB; //GSM-GPRS
		else
			buffer[index++] = (byte)0xAA; //GSM-CSD
		buffer[index++] = (byte)0x01; //end of param element
		//<param name="NAME" value="xxxx"/>
		buffer[index++] = (byte)0x87; //start of param
		buffer[index++] = (byte)0x07; //name=NAME
		buffer[index++] = (byte)0x06; //value=
		buffer[index++] = (byte)0x03; //start of inline string
		System.arraycopy(name.getBytes(), 0, buffer, index, name.length());
		index += name.length();
		buffer[index++] = (byte)0x00; //end of inline string
		buffer[index++] = (byte)0x01; //end of param element
		//<param name="NAP-ADDRESS" value="xxxx"/>
		buffer[index++] = (byte)0x87; //start of param
		buffer[index++] = (byte)0x08; //name=NAP-ADDRESS
		buffer[index++] = (byte)0x06; //value=
		buffer[index++] = (byte)0x03; //start of inline string
		System.arraycopy(nap_address.getBytes(), 0, buffer, index, nap_address.length());
		index += nap_address.length();
		buffer[index++] = (byte)0x00; //end of inline string
		buffer[index++] = (byte)0x01; //end of param element
		//<param name="NAP-ADDRTYPE" value=""/>
		buffer[index++] = (byte)0x87; //start of param
		buffer[index++] = (byte)0x09; //name=NAP-ADDRTYPE
		buffer[index++] = (byte)0x06; //value=
		if(bearer.equals("GSM-GPRS"))
		{
			buffer[index++] = (byte)0x89; //APN
		}
		else //GSM-CSD
		{
			buffer[index++] = (byte)0x87; //E164
		}
		buffer[index++] = (byte)0x01; //end of param element
		if(bearer.equals("GSM-CSD"))
		{
			//<param name="CALLTYPE" value=""/>
			buffer[index++] = (byte)0x87; //start of param
			buffer[index++] = (byte)0x0A; //name=CALLTYPE
			buffer[index++] = (byte)0x06; //value=
			buffer[index++] = (byte)0x92; //V.110
			buffer[index++] = (byte)0x01; //end of param element
			//<param name="LINKSPEED" value="xxxx"/>
			buffer[index++] = (byte)0x87; //start of param
			buffer[index++] = (byte)0x25; //name=LINKSPEED
			buffer[index++] = (byte)0x06; //value=
			buffer[index++] = (byte)0x03; //start of inline string
			System.arraycopy(linkspeed.getBytes(), 0, buffer, index, linkspeed.length());
			index += linkspeed.length();
			buffer[index++] = (byte)0x00; //end of inline string
			buffer[index++] = (byte)0x01; //end of param element
		}
		
//		System.out.println("DNS 1 : " + dns1);
//		System.out.println("DNS 2 : " + dns2);
		if(dns1!=null)
		{
			//<param name="DNS-ADDR" value=""/>
			buffer[index++] = (byte)0x87; //start of param
			buffer[index++] = (byte)0x48; //name=DNS-ADDR
			buffer[index++] = (byte)0x06; //value=
			buffer[index++] = (byte)0x03; //start of inline string
			System.arraycopy(dns1.getBytes(), 0, buffer, index, dns1.length());
			index += dns1.length();
			buffer[index++] = (byte)0x00; //end of inline string
			buffer[index++] = (byte)0x01; //end of param element
		}
		if(dns2!=null)
		{
			//<param name="DNS-ADDR" value=""/>
			buffer[index++] = (byte)0x87; //start of param
			buffer[index++] = (byte)0x48; //name=DNS-ADDR
			buffer[index++] = (byte)0x06; //value=
			buffer[index++] = (byte)0x03; //start of inline string
			System.arraycopy(dns2.getBytes(), 0, buffer, index, dns2.length());
			index += dns2.length();
			buffer[index++] = (byte)0x00; //end of inline string
			buffer[index++] = (byte)0x01; //end of param element
		}
		//<characteristic type="NAPAUTHINFO">
		buffer[index++] = (byte)0xC6; //characteristic
		buffer[index++] = (byte)0x5A; //type=NAPAUTHINFO
		buffer[index++] = (byte)0x01; //start of characteristic with type=NAPAUTHINFO
		//<param name="AUTHTYPE" value=""/>
		buffer[index++] = (byte)0x87; //start of param
		buffer[index++] = (byte)0x0C; //name=AUTHTYPE
		buffer[index++] = (byte)0x06; //value=	
		if(napauthinfo.equals("PAP"))
		{
			buffer[index++] = (byte)0x9A; //PAP
		}
		else
			buffer[index++] = (byte)0x9B; //CHAP		
		buffer[index++] = (byte)0x01; //end of param element
		if(authname!=null)
		{
			//<param name="AUTHNAME" value="xxxx"/>
			buffer[index++] = (byte)0x87; //start of param
			buffer[index++] = (byte)0x0D; //name=AUTHNAME
			buffer[index++] = (byte)0x06; //value=
			buffer[index++] = (byte)0x03; //start of inline string
			System.arraycopy(authname.getBytes(), 0, buffer, index, authname.length());
			index += authname.length();
			buffer[index++] = (byte)0x00; //end of inline string
			buffer[index++] = (byte)0x01; //end of param element
		}
		if(authsecret!=null)
		{
			//<param name="AUTHSECRET" value="xxxx"/>
			buffer[index++] = (byte)0x87; //start of param
			buffer[index++] = (byte)0x0E; //name=AUTHSECRET
			buffer[index++] = (byte)0x06; //value=
			buffer[index++] = (byte)0x03; //start of inline string
			System.arraycopy(authsecret.getBytes(), 0, buffer, index, authsecret.length());
			index += authsecret.length();
			buffer[index++] = (byte)0x00; //end of inline string
			buffer[index++] = (byte)0x01; //end of param element
		}
		buffer[index++] = (byte)0x01; //end of characteristic with type=NAPAUTHINFO  
		buffer[index++] = (byte)0x01; //end of characteristic with type=NAPDEF
		
		return index - startPos;
	}
}
